package com.springboot.demo.read.file;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * 大文件读取
 *
 * @author supu
 * @date 2020-03-20 17:49
 **/
public class BigFileRead {
    public static void readBigFile() {
        String file = "c:/test.txt";
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(file)));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream, "utf-8"), 10 * 1024 * 1024);
            FileWriter fileWriter = new FileWriter("d:/test.txt");
            while (bufferedReader.ready()) {
                String line = bufferedReader.readLine();
                fileWriter.append(line + "");
            }
            bufferedReader.close();
            fileWriter.flush();
            fileWriter.close();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
